package com.weilai.controller;

import com.weilai.controller.exceptionHandler.BusinessException;
import com.weilai.controller.exceptionHandler.Code;
import com.weilai.controller.exceptionHandler.SystemException;
import com.weilai.domain.*;

import com.weilai.service.PostsService;
import com.weilai.service.RepliesService;
import com.weilai.service.UserService;
import com.weilai.service.VideosService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@Api(tags = "创作中心", description = "")
public class CreativeCenterController {

    @Autowired
    private UserService userService;
    @Autowired
    private PostsService postsService;
    @Autowired
    private VideosService videosService;

    @ApiOperation(value = "跳转创作中心，也是获取所有数据", notes = "")
    //跳转创作中心
    @GetMapping("/creativeCenter")
    public ModelAndView creativeCenter(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");

        // 检查登录状态
        if(user == null){
            throw new BusinessException(
                    Code.PROJECT_ACCESS_DENIED,
                    "请先登录"
            );
        }

        try {
            mav.addObject("user", user);

            // 验证用户是否存在
            User dbUser = userService.getUserById(user.getId());
            if(dbUser == null) {
                throw new BusinessException(
                        Code.PROJECT_DATA_NOT_FOUND,
                        "用户数据不存在"
                );
            }

            // 处理文章数据
            List<Posts> article = postsService.selectData(user.getId());
            int articleLikeNum = 0, articleCommentNum = 0, articleShareNum = 0, articleViewNum = 0;
            for(Posts p : article) {
                articleLikeNum += p.getLikeCount();
                articleCommentNum += p.getCommentCount();
                articleShareNum += p.getShareCount();
                articleViewNum += p.getViewCount();
            }

            // 处理视频数据
            List<Videos> videos = videosService.selectData(user.getId());
            int videosLikeNum = 0, videosCommentNum = 0, videosShareNum = 0, videosViewNum = 0;
            for(Videos v : videos) {
                videosLikeNum += v.getLikeCount();
                videosCommentNum += v.getCommentCount();
                videosShareNum += v.getShareCount();
                videosViewNum += v.getViewCount();
            }

            // 添加文章数据到视图
            mav.addObject("articleLikeNum", articleLikeNum);
            mav.addObject("articleCommentNum", articleCommentNum);
            mav.addObject("articleShareNum", articleShareNum);
            mav.addObject("articleViewNum", articleViewNum);

            // 添加视频数据到视图
            mav.addObject("videosLikeNum", videosLikeNum);
            mav.addObject("videosCommentNum", videosCommentNum);
            mav.addObject("videosShareNum", videosShareNum);
            mav.addObject("videosViewNum", videosViewNum);
            mav.addObject("time", LocalDateTime.now());
            mav.addObject("user", user);

            mav.setViewName("creatorCenterMainPage");
        } catch (Exception e) {
            throw new SystemException(
                    Code.SYSTEM_DB_ERROR,
                    "获取创作中心数据失败",
                    e
            );
        }
        return mav;
    }

//    @ApiOperation(value = "获取文章数据", notes = "")
//    //获取文章各种数据
//    @GetMapping("/article")
//    public ModelAndView posts(HttpServletRequest request) {
//        ModelAndView mav = new ModelAndView("creativeCenter");
//        HttpSession session = request.getSession();
//        Integer id = (Integer) session.getAttribute("id");
//
//        // 检查登录状态
//        if(id == null) {
//            throw new BusinessException(
//                    Code.PROJECT_ACCESS_DENIED,
//                    "请先登录"
//            );
//        }
//
//        try {
//            // 验证用户是否存在
//            User user = userService.getUserById(id);
//            if(user == null) {
//                throw new BusinessException(
//                        Code.PROJECT_DATA_NOT_FOUND,
//                        "用户数据不存在"
//                );
//            }
//
//            List<Posts> article = postsService.selectByUserIdAndArticle(id, "文章");
//
//            // 计算统计数据
//            int articleLikeNum = 0, articleCommentNum = 0, articleShareNum = 0, articleViewNum = 0;
//            for(Posts p : article) {
//                articleLikeNum += p.getLikeCount();
//                articleCommentNum += p.getCommentCount();
//                articleShareNum += p.getShareCount();
//                articleViewNum += p.getViewCount();
//            }
//
//            // 添加统计数据到视图
//            mav.addObject("articleLikeNum", articleLikeNum);
//            mav.addObject("articleCommentNum", articleCommentNum);
//            mav.addObject("articleShareNum", articleShareNum);
//            mav.addObject("articleViewNum", articleViewNum);
//        } catch (Exception e) {
//            throw new SystemException(
//                    Code.SYSTEM_DB_ERROR,
//                    "获取文章数据失败",
//                    e
//            );
//        }
//        return mav;
//    }
//
//    @ApiOperation(value = "获取视频数据", notes = "")
//    //获取视频各种数据
//    @GetMapping("/videos")
//    public ModelAndView videos(HttpServletRequest request) {
//        ModelAndView mav = new ModelAndView("creativeCenter");
//        HttpSession session = request.getSession();
//        Integer id = (Integer) session.getAttribute("id");
//
//        // 检查登录状态
//        if(id == null) {
//            throw new BusinessException(
//                    Code.PROJECT_ACCESS_DENIED,
//                    "请先登录"
//            );
//        }
//
//        try {
//            // 验证用户是否存在
//            User user = userService.getUserById(id);
//            if(user == null) {
//                throw new BusinessException(
//                        Code.PROJECT_DATA_NOT_FOUND,
//                        "用户数据不存在"
//                );
//            }
//
//            List<Videos> videos = videosDao.selectByUserId(id);
//
//            // 计算统计数据
//            int videosLikeNum = 0, videosCommentNum = 0, videosShareNum = 0, videosViewNum = 0;
//            for(Videos v : videos) {
//                videosLikeNum += v.getLikeCount();
//                videosCommentNum += v.getCommentCount();
//                videosShareNum += v.getShareCount();
//                videosViewNum += v.getViewCount();
//            }
//
//            // 添加统计数据到视图
//            mav.addObject("videosLikeNum", videosLikeNum);
//            mav.addObject("videosCommentNum", videosCommentNum);
//            mav.addObject("videosShareNum", videosShareNum);
//            mav.addObject("videosViewNum", videosViewNum);
//        } catch (Exception e) {
//            throw new SystemException(
//                    Code.SYSTEM_DB_ERROR,
//                    "获取视频数据失败",
//                    e
//            );
//        }
//        return mav;
//    }
}
